package com.CampusLife.controller;

import  com.CampusLife.bean.Favorite;
import com.CampusLife.dto.Page;
import com.CampusLife.service.IProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import  com.CampusLife.service.IFavoriteService;
import  com.CampusLife.until.DataTime;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/favorite")
public class FavoriteController {
    @Resource
    private IFavoriteService iFavoriteService;
    @Resource
    private IProductService  iProductService;

    @RequestMapping("/user_favorite")
    public  String  user_favorite(HttpSession httpSession,Model model){


        int userId=(int)httpSession.getAttribute("id");
        List<Favorite>  favorites=iFavoriteService.getFavoriteAll(userId);
        model.addAttribute("favorites",favorites);
        return  "home/userfavortie";
    }
    @RequestMapping("/add")
    public String addcomment( int pid, HttpSession httpSession, Model model){
        int i=(int) httpSession.getAttribute("id");
        Favorite favorite=new Favorite();
        favorite.setP_id(pid);
        favorite.setId(i);
      int num= iFavoriteService.queryByPid(favorite);
      if (num == 0) {
          DataTime dataTime=new DataTime();
          String data=dataTime.gettime();
          favorite.setTime(data);
          iFavoriteService.add(favorite);
          return "redirect:/favorite/user_favorite";
      }
      else {
           return "redirect:/favorite/user_favorite";
      }
    }
    @RequestMapping("/delete")
    public  String delete(Integer fid){
        iFavoriteService.delete(fid);
        return "redirect:/favorite/user_favorite";
    }


}


